package com.woniuxy.boot.nb_auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.boot.common.bean.EsUser;
import com.woniuxy.boot.common.dto.EsUserSearchDTO;
import com.woniuxy.boot.common.dto.UserDTO;
import com.woniuxy.boot.nb_auth.bean.Admin;
import com.woniuxy.boot.nb_auth.bean.User;
import com.woniuxy.boot.nb_auth.feign.UserSearchFeign;
import com.woniuxy.boot.nb_auth.mapper.AdminMapper;
import com.woniuxy.boot.nb_auth.mapper.UserMapper;
import com.woniuxy.boot.nb_auth.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private UserSearchFeign userSearchFeign;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Admin getAdminByAccount(String account) {
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getAccount, account);
        return adminMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public List<UserDTO> getAllUser() {
        List<User> userList = userMapper.selectList(null);
        List<UserDTO> userDTOList = new ArrayList<>();
        for (User user : userList) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user, userDTO);
            userDTOList.add(userDTO);
        }
        return userDTOList;
    }

    @Override
    public List<EsUser> getByCondition(List<EsUser> esUsers,EsUserSearchDTO esUserSearchDTO) {
        // 使用流API一次性处理所有过滤条件
        esUsers = esUsers.stream()
                .filter(esUser -> {
                    boolean match = true;

                    // 如果 idBuy 不为空且不为空字符串，则检查 isBuy 属性
                    if (esUserSearchDTO.getIdBuy() != null && !esUserSearchDTO.getIdBuy().isEmpty()) {
                        match &= Objects.equals(esUser.getIsBuy(), esUserSearchDTO.getIdBuy());
                    }

                    // 如果 status 不为空且不为空字符串，则检查 status 属性
                    if (esUserSearchDTO.getStatus() != null && !esUserSearchDTO.getStatus().isEmpty()) {
                        match &= Objects.equals(esUser.getStatus(), esUserSearchDTO.getStatus());
                    }

                    // 如果 minOrderNum 不为空，则检查 policyNum 属性
                    if (esUserSearchDTO.getMinOrderNum() != null) {
                        Long policyNum = esUser.getPolicyNum();
                        match &= policyNum != null && policyNum > esUserSearchDTO.getMinOrderNum();
                    }

                    // 如果 maxOrderNum 不为空，则检查 policyNum 属性
                    if (esUserSearchDTO.getMaxOrderNum() != null) {
                        Long policyNum = esUser.getPolicyNum();
                        match &= policyNum != null && policyNum < esUserSearchDTO.getMaxOrderNum();
                    }

                    return match;
                })
                .collect(Collectors.toList()); // 使用 collect 而不是 toList 以兼容旧版本的 Java
        return esUsers;
    }
}